<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/14
 * Time: 8:52
 */

namespace app\ext\model;


use think\Model;

class DemoModel extends Model
{
    public function addB($name = '')
    {
        if(empty($name))return false;
        $data = [
            'name'  =>  $name,
            'tranname'  =>  $name,
            'add_time'   =>  time(),
        ];

        return db('book')->insertGetId($data);
    }

    public function completeB($id = 0)
    {
        $rs = db('book')->where(['id'=>$id])->update(['is_complete'=>1]);
        return $rs;
    }

    public function clearB($id = 0)
    {
        return db('bcontent')->where(['b_id'=>$id])->delete();
    }

    public function bList($id = 0)
    {
        if($id)
        {
            return db('book')->where("id =$id")->find();
        }else{
            return db('book')->select();
        }
    }


    public function addBC($bid = 0, $content = '', $sort = 0)
    {
        if($sort == 0 )return false;
        $data = [
            'b_id'      =>  $bid,
            'content'       =>  $content,
            'sort'      =>  $sort,
            'add_time'      =>  time()
        ];
        $rs = db('bcontent')->insertGetId($data);
        return $rs;
    }

    public function isLeadIn($name)
    {
        $isLeadIn = db('book')->where(" (name = '".$name."' or tranname ='". $name."') and is_complete = 1")->find();
        return empty($isLeadIn) ? 0 : $isLeadIn['id'];
    }
}